package com.project.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.auth.model.system.SysRole;
import com.project.auth.service.SysRoleService;
import com.project.auth.vo.system.AssginRoleVo;
import com.project.auth.vo.system.SysRoleQueryVo;
import com.project.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@EnableGlobalMethodSecurity//开启基于方法的安全认证机制，也就是说在web层的controller启用注解机制的安全确认
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId){
        Map<String,Object> map = sysRoleService.findRoleDataByUserId(userId);

        return Result.ok(map);
    }

    @ApiOperation("分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);

        return Result.ok();
    }


    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll() {
        /*//模拟异常效果
        try{
            int a = 10/0;
        }catch (Exception e){
            throw new MyException(123321,"执行自定义异常处理...");
        }*/

        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }


    //条件分页查询
    @ApiOperation("条件分页查询")
    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> iPage = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        //判断条件,非空则封装
        if (!StringUtils.isEmpty(roleName)) {
            //使用like模糊查询
            wrapper.like(SysRole::getRoleName, roleName);
        }
        return Result.ok(sysRoleService.page(iPage, wrapper));
    }

    //添加角色接口
    @ApiOperation("添加角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        boolean success = sysRoleService.save(sysRole);
        if (success) {
            return Result.ok().message("添加成功");
        }
        return Result.fail().message("添加失败");
    }

    //按id查询角色
    @ApiOperation("按id查询角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        if (!StringUtils.isEmpty(id)) {
            SysRole sysRole = sysRoleService.getById(id);
            return Result.ok(sysRole).message("查询成功!");
        }
        return Result.fail().message("未查询到该角色!");
    }

    //修改角色
    @ApiOperation("修改角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {
        boolean success = sysRoleService.updateById(sysRole);
        if (success) {
            return Result.ok().message("修改成功!");
        }
        return Result.fail().message("修改失败!");
    }

    //根据id删除
    @ApiOperation("根据id删除角色")
    //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/remove{id}")
    public Result remove(@PathVariable Long id) {
        boolean success = sysRoleService.removeById(id);
        if (success){
            return Result.ok().message("删除成功!");
        }
        return Result.fail().message("删除失败!");
    }

    //批量删除
    @ApiOperation("批量删除角色")
   //@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean success = sysRoleService.removeByIds(idList);
        if (success){
            return Result.ok().message("删除成功!");
        }
        return Result.fail().message("删除失败!");
    }
}
